﻿using System;
using System.Collections.Generic;

using System.Text;
using Xeno.Core;
namespace Xeno.Filter
{
    public class FilterBloom : XenoFilter
    {
        public XenoMaterial ColorExtract { get; set; }
        public XenoTexture2D DownSample { get; set; }
        public XenoMaterial BloomMaterial { get; set; }
        public XenoTexture2D WorkMap { get; set; }
        public FilterBloom(int res)
            : base(res)
        {
            ColorExtract = new XenoMaterial("ColorExtract", XenoMaterial.MaterialFlags.X2D);
            ColorExtract.Effect = new XenoEffect("", "Content/Shader/XenoMaterial_Extract.fragment");
            DownSample = new XenoTexture2D((int)XenoCore.DisplayWidth/2,(int)XenoCore.DisplayHeight/2);
            WorkMap = new XenoTexture2D((int)XenoCore.DisplayWidth, (int)XenoCore.DisplayHeight);
            BloomMaterial = new XenoMaterial("Bloom");
            BloomMaterial.Effect = new XenoEffect("", "Content/Shader/XenoMaterial_Bloom.fragment");
        }

        public override XenoTexture2D DoRender()
        {

            ColorExtract.Bind();
            ColorExtract.Effect.SetFloat("MinLevel",0.35f);
            ColorExtract.Unbind();
            this.BufferToBuffer(FrameBuffer, DownSample, ColorExtract);

            this.BlurBuffer(DownSample, WhiteMap, WorkMap);

            BloomMaterial.NormalMap = WorkMap;
            this.BufferToBuffer(FrameBuffer, Output, BloomMaterial);

            //this.BufferToBuffer(DownSample, Output, TransferMat);

            return Output;
        }

    }
}
